const CustomerCoupon = require("../models/customerCouponModel");
const Customer = require("../models/customerDataModel");
const DiscountCoupon = require("../models/discountCouponModel");

exports.createCustomerCoupon = async (req, res) => {
  try {
    const {
      customer,
      discountCoupon,
      couponCode,
      usedDate,
      status,
      occasionName,
      expiryDate,
      value,
      couponType,
      description,
    } = req.body;

    
    if (!customer || !discountCoupon) {
      return res.status(400).json({ message: "Customer and discountCoupon are required!" });
    }

    // Validate references
    const customerExists = await Customer.findById(customer);
    if (!customerExists) {
      return res.status(404).json({ message: "Customer not found!" });
    }

    const couponExists = await DiscountCoupon.findById(discountCoupon);
    if (!couponExists) {
      return res.status(404).json({ message: "Discount coupon not found!" });
    }

    // Create a new customer coupon
    const newCustomerCoupon = new CustomerCoupon({
      customer,
      discountCoupon,
      couponCode: couponCode || couponExists.couponCode, // default to main coupon code if not provided
      usedDate: usedDate || null,
      status: status || "Active",
      occasionName: occasionName || couponExists.occasionName,
      expiryDate: expiryDate || couponExists.expiryDate,
      value: value || couponExists.value,
      couponType: couponType || couponExists.couponType,
      description: description || couponExists.description,
    });

    await newCustomerCoupon.save();
    res.status(201).json({ message: "Customer coupon created!", customerCoupon: newCustomerCoupon });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};

exports.getAllCustomerCoupons = async (req, res) => {
  try {
    const customerCoupons = await CustomerCoupon.find()
      .populate("customer discountCoupon");
    res.status(200).json(customerCoupons);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};

exports.getCustomerCouponById = async (req, res) => {
  try {
    const coupon = await CustomerCoupon.findById(req.params.id)
      .populate("customer discountCoupon");
    if (!coupon) {
      return res.status(404).json({ message: "Customer coupon not found!" });
    }
    res.status(200).json(coupon);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};

exports.updateCustomerCoupon = async (req, res) => {
  try {
    const { id } = req.params;
    const updated = await CustomerCoupon.findByIdAndUpdate(id, req.body, { new: true })
      .populate("customer discountCoupon");
    if (!updated) {
      return res.status(404).json({ message: "Customer coupon not found!" });
    }
    res.status(200).json({ message: "Customer coupon updated!", customerCoupon: updated });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};

exports.deleteCustomerCoupon = async (req, res) => {
  try {
    const { id } = req.params;
    const coupon = await CustomerCoupon.findByIdAndDelete(id);
    if (!coupon) {
      return res.status(404).json({ message: "Customer coupon not found!" });
    }
    res.status(200).json({ message: "Customer coupon deleted successfully!" });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: "Internal server error" });
  }
};
